/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import hyper.market.beans.NotificationBean;
import hyper.market.beans.StockBean;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mikeno
 */
public class EditItemInformationServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Object id = request.getSession().getAttribute("id");
        if (id == null) {
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        int stockid = -1;
        String name = request.getParameter("itemname");
        String expiry = request.getParameter("expiry");
        String mySqlJndiName = this.getServletContext().getInitParameter("MySql JNDI");
        int threshold = 0, quantity = 0;
        double cost = 0, selling = 0, discount = 0;
        int storeid = 0;

        if (name.equals("") || name == null) {
            request.getRequestDispatcher("/web_edit/fail.html").forward(request, response);
        }
        try {
            stockid = Integer.parseInt(request.getParameter("stockid"));
            quantity = Integer.parseInt(request.getParameter("quantity"));
            threshold = Integer.parseInt(request.getParameter("threshold"));
            cost = Double.parseDouble(request.getParameter("cost"));
            selling = Double.parseDouble(request.getParameter("selling"));
            storeid = Integer.parseInt(request.getParameter("store"));
            discount = Double.parseDouble(request.getParameter("discount"));
        } catch (NumberFormatException e) {
            request.getRequestDispatcher("/web_edit/fail.html").forward(request, response);
        }

        StockBean stock = new StockBean(stockid, name, cost, selling, threshold, quantity, discount, expiry);
        stock.updateItemDetails(mySqlJndiName, stock, storeid);


        request.getRequestDispatcher("/web_edit/Success.html").forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
